package cst.a113cx.recyclerview02;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pc on 2017/5/15.
 */

public class MyRecyclerAdapter2 extends RecyclerView.Adapter<MyRecyclerAdapter2.MyViewHolder> {
    private ArrayList<Abean> mDatas=new ArrayList<Abean>();
    private Context mContext;
    private LayoutInflater inflater;

    public MyRecyclerAdapter2(Context context, ArrayList<Abean> data00){
        this. mContext=context;
        this. mDatas=data00;
        inflater=LayoutInflater. from(mContext);
    }

    @Override
    public int getItemCount() {

        return mDatas.size();
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        holder.tv1.setText( mDatas.get(position).getTitle());
        holder.tv2.setText(mDatas.get(position).getDescription());
        holder.tv3.setText(mDatas.get(position).getTime());
        holder.imageView.setImageResource(mDatas.get(position).getImgid());
        holder.b01.setText( "删除");
        holder.b01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int t=0;t<=mDatas.size()-1;t++){
                    if(mDatas.get(t).getTitle()==holder.tv1.getText()){
                        mDatas.remove(t);
                        notifyItemRemoved(t);
                    }
                }


            }
        });
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

  /*UNKNOW*/        View view = inflater.inflate(R.layout.item2,parent, false);
        MyViewHolder holder= new MyViewHolder(view);
        return holder;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        //TextView tv;
        ImageView imageView;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        Button b01;

        public MyViewHolder(View view) {
            super(view);
            imageView=(ImageView) view.findViewById(R.id.iv);
            tv1=(TextView) view.findViewById(R.id. tv1);
            tv2=(TextView) view.findViewById(R.id.tv2);
            tv3=(TextView) view.findViewById(R.id.tv3);
            b01=(Button) view.findViewById(R.id.b01);


        }

    }
}
